# Author: HZT
# Date: 2023/11/6
import pyautogui
from config.config_loader import config
from page.web_ck_df import WebCkDf
from page.web_df_jd import WebDfJd
from page.web_df_top import WebDfTop
from page.web_gl_km import WebGlKm
from page.web_sk_jk import WebSkJk
from page.web_ck_jy import WebCkJy
from page.web_ck_top import WebCkTop
from page.web_sk_top import WebSkTop
from utils import UtilsDriver, login_with_session, GetLog, UtilsDriver2, get_db_ye, UtilsDriver3, get_db_ye_df, \
    UtilsDriver4, login_with_session_df
from page.web_ck_dd import WebCkDd
import time


class TestWebYhhd:
    card_id_cg = None
    card_id_sb = None
    driver3 = None  # 作为卡商管理的驱动
    glKm = None
    driver4 = None
    dfJd = None
    dfTop = None

    def setup_class(self):
        self.log = GetLog.get_log()
        self.driver = UtilsDriver.get_web_driver()
        self.driver.get(config.URL)
        login_with_session('system2', self.driver)
        self.driver2 = UtilsDriver2.get_web_driver()
        self.driver2.get(config.URL)
        login_with_session('system1', self.driver2)
        # 实例化
        self.ckJy = WebCkJy(self.driver2)
        self.ckTop = WebCkTop(self.driver2)
        self.ckDd = WebCkDd(self.driver2)
        self.skJk = WebSkJk(self.driver)
        self.skTop = WebSkTop(self.driver)
        self.ckDf = WebCkDf(self.driver2)

    def teardown_class(self):
        # 在所有测试用例执行完毕后，关闭浏览器
        # UtilsDriver.get_web_driver().quit()
        pass

    # 转账交易(成功) 转账金额：500
    def test01_yehd(self):
        try:
            # 获取转帐前余额
            ckye_before = get_db_ye(config.CREDENTIALS["username_ck"])
            skye_before = get_db_ye(config.CREDENTIALS["username_sk"])
            self.ckTop.ck_top_click_zz()
            time.sleep(1)
            self.ckTop.ck_top_zz("SK64E442563BE14", "500", "转账！", self.driver2)
            self.ckTop.ck_top_click_zzqd()
            # 获取转账弹窗文本
            time.sleep(0.5)
            zztc = self.ckTop.ck_top_get_zztc()
            assert "转账成功" in zztc
            # 获取转帐后余额
            ckye_after = get_db_ye(config.CREDENTIALS["username_ck"])
            skye_after = get_db_ye(config.CREDENTIALS["username_sk"])
            assert (float(ckye_before) - float(ckye_after)) == 500.0
            assert (float(skye_before) - float(skye_after)) == -500.0
        except Exception as e:
            self.log.error("错误---,原因{}".format(e))
            # 截图
            self.ckTop.base_get_img()
            # 抛异常
            raise

    # 卡片交易(成功) 金额100，汇率8.8
    def test02_yehd(self):
        try:
            pyautogui.hotkey('alt', 'tab')
            self.skJk.sk_jk_click_jk()
            # 定位开关元素
            switch = self.skJk.sk_jk_get_ksjk()
            # 判断开关状态
            if switch.get_attribute("aria-hidden") == "true":
                # 开关是关闭状态，点击开关
                self.skJk.sk_jk_click_ksjk()
            pyautogui.hotkey('alt', 'tab')
            time.sleep(2)
            count = 1  # 发卡数
            me = '100'  # 发卡金额
            self.ckDd.ck_dd_click_jdd()
            self.ckDd.ck_dd_ck(self.driver2, me, "sephora_card1.png", count, "t-Sephora")
            # 获取前余额
            ckye_before = get_db_ye(config.CREDENTIALS["username_ck"])
            skye_before = get_db_ye(config.CREDENTIALS["username_sk"])
            pyautogui.hotkey('alt', 'tab')
            time.sleep(2)
            self.skTop.sk_top_click_sx()
            self.skJk.sk_jk_click_ckkm()
            self.skJk.sk_jk_get_clcg()
            TestWebYhhd.card_id_cg = self.skJk.sk_jk_get_ids()[0].text
            self.skJk.sk_jk_click_clcg()
            self.skJk.sk_jk_click_cgtj()
            self.skTop.sk_top_click_sx()
            # 获取后余额
            ckye_after = get_db_ye(config.CREDENTIALS["username_ck"])
            skye_after = get_db_ye(config.CREDENTIALS["username_sk"])
            assert (float(ckye_before) - float(ckye_after)) == -836.0
            assert (float(skye_before) - float(skye_after)) == 880.0
        except Exception as e:
            self.log.error("错误---,原因{}".format(e))
            # 截图
            self.ckTop.base_get_img()
            # 抛异常
            raise

    # 卡片交易(失败)
    def test03_yehd(self):
        try:
            pyautogui.hotkey('alt', 'tab')
            time.sleep(2)
            count = 1  # 发卡数
            me = '100'  # 发卡金额
            self.ckDd.ck_dd_click_jdd()
            self.ckDd.ck_dd_ck(self.driver2, me, "sephora_card1.png", count, "t-Sephora")
            pyautogui.hotkey('alt', 'tab')
            # 获取前余额
            ckye_before = get_db_ye(config.CREDENTIALS["username_ck"])
            skye_before = get_db_ye(config.CREDENTIALS["username_sk"])
            time.sleep(2)
            self.skTop.sk_top_click_sx()
            self.skJk.sk_jk_click_ckkm()
            self.skJk.sk_jk_get_clcg()
            TestWebYhhd.card_id_sb = self.skJk.sk_jk_get_ids()[0].text
            self.skJk.sk_jk_click_flp()
            self.skJk.sk_jk_click_szsb()
            self.skJk.sk_jk_get_szsbtc()
            # 获取后余额
            ckye_after = get_db_ye(config.CREDENTIALS["username_ck"])
            skye_after = get_db_ye(config.CREDENTIALS["username_sk"])
            assert (float(ckye_before) - float(ckye_after)) == 0.0
            assert (float(skye_before) - float(skye_after)) == 0.0
        except Exception as e:
            self.log.error("错误---,原因{}".format(e))
            # 截图
            self.ckTop.base_get_img()
            # 抛异常
            raise
        finally:
            self.driver.quit()

    # 卡片交易(编辑) 金额100，汇率4.5
    def test04_yehd(self):
        try:
            TestWebYhhd.driver3 = UtilsDriver3.get_web_driver()
            TestWebYhhd.driver3.get(config.URL)
            login_with_session('system3', TestWebYhhd.driver3)
            TestWebYhhd.driver3.get(f"http://1.117.224.100:81/admin/system/order-cards/{TestWebYhhd.card_id_cg}/edit")
            TestWebYhhd.glKm = WebGlKm(TestWebYhhd.driver3)
            # 获取前余额
            ckye_before = get_db_ye(config.CREDENTIALS["username_ck"])
            skye_before = get_db_ye(config.CREDENTIALS["username_sk"])
            TestWebYhhd.glKm.gl_km_input_bjhl("4.5")
            TestWebYhhd.glKm.gl_km_click_bjtj()
            TestWebYhhd.glKm.gl_km_get_kpid()
            # 获取后余额
            ckye_after = get_db_ye(config.CREDENTIALS["username_ck"])
            skye_after = get_db_ye(config.CREDENTIALS["username_sk"])
            assert (float(ckye_before) - float(ckye_after)) == 408.5
            assert (float(skye_before) - float(skye_after)) == -430.0
        except Exception as e:
            self.log.error("错误---,原因{}".format(e))
            # 截图
            self.ckTop.base_get_img()
            # 抛异常
            raise

    # 卡片交易(再次编辑) 金额50，汇率4.5
    def test05_yehd(self):
        try:
            TestWebYhhd.driver3.get(f"http://1.117.224.100:81/admin/system/order-cards/{TestWebYhhd.card_id_cg}/edit")
            # 获取前余额
            ckye_before = get_db_ye(config.CREDENTIALS["username_ck"])
            skye_before = get_db_ye(config.CREDENTIALS["username_sk"])
            TestWebYhhd.glKm.gl_km_input_bjme("50")
            TestWebYhhd.glKm.gl_km_click_bjtj()
            TestWebYhhd.glKm.gl_km_get_kpid()
            # 获取后余额
            ckye_after = get_db_ye(config.CREDENTIALS["username_ck"])
            skye_after = get_db_ye(config.CREDENTIALS["username_sk"])
            assert (float(ckye_before) - float(ckye_after)) == 213.75
            assert (float(skye_before) - float(skye_after)) == -225.0
        except Exception as e:
            self.log.error("错误---,原因{}".format(e))
            # 截图
            self.ckTop.base_get_img()
            # 抛异常
            raise

    # 卡片交易(失败手动成功) 金额100，汇率8.8
    def test06_yehd(self):
        try:
            # 获取前余额
            ckye_before = get_db_ye(config.CREDENTIALS["username_ck"])
            skye_before = get_db_ye(config.CREDENTIALS["username_sk"])
            TestWebYhhd.glKm.gl_km_click_bj(TestWebYhhd.card_id_sb)
            time.sleep(1)
            TestWebYhhd.glKm.gl_km_click_sdcg(TestWebYhhd.card_id_sb)
            TestWebYhhd.glKm.gl_km_click_cgtj()
            TestWebYhhd.glKm.gl_km_click_czwc()
            time.sleep(1)
            # 获取后余额
            ckye_after = get_db_ye(config.CREDENTIALS["username_ck"])
            skye_after = get_db_ye(config.CREDENTIALS["username_sk"])
            assert (float(ckye_before) - float(ckye_after)) == -836.0
            assert (float(skye_before) - float(skye_after)) == 880.0
        except Exception as e:
            self.log.error("错误---,原因{}".format(e))
            # 截图
            self.ckTop.base_get_img()
            # 抛异常
            raise

    # 卡片交易(成功之后再次编辑) 金额100，汇率7.8
    def test07_yehd(self):
        try:
            TestWebYhhd.driver3.get(f"http://1.117.224.100:81/admin/system/order-cards/{TestWebYhhd.card_id_sb}/edit")
            # 获取前余额
            ckye_before = get_db_ye(config.CREDENTIALS["username_ck"])
            skye_before = get_db_ye(config.CREDENTIALS["username_sk"])
            TestWebYhhd.glKm.gl_km_input_bjhl("7.8")
            TestWebYhhd.glKm.gl_km_click_bjtj()
            TestWebYhhd.glKm.gl_km_get_kpid()
            # 获取后余额
            ckye_after = get_db_ye(config.CREDENTIALS["username_ck"])
            skye_after = get_db_ye(config.CREDENTIALS["username_sk"])
            assert (float(ckye_before) - float(ckye_after)) == 95.0
            assert (float(skye_before) - float(skye_after)) == -100.0
        except Exception as e:
            self.log.error("错误---,原因{}".format(e))
            # 截图
            self.ckTop.base_get_img()
            # 抛异常
            raise

    # 卡片交易(编辑之后关闭订单)
    def test08_yehd(self):
        try:
            # 获取前余额
            ckye_before = get_db_ye(config.CREDENTIALS["username_ck"])
            skye_before = get_db_ye(config.CREDENTIALS["username_sk"])
            TestWebYhhd.glKm.gl_km_click_bj(TestWebYhhd.card_id_sb)
            TestWebYhhd.glKm.gl_km_click_gbdd(TestWebYhhd.card_id_sb)
            TestWebYhhd.glKm.gl_km_click_cgtj()
            TestWebYhhd.glKm.gl_km_click_czwc()
            time.sleep(2)
            # 获取后余额
            ckye_after = get_db_ye(config.CREDENTIALS["username_ck"])
            skye_after = get_db_ye(config.CREDENTIALS["username_sk"])
            assert (float(ckye_before) - float(ckye_after)) == 741.0
            assert (float(skye_before) - float(skye_after)) == -780.0
        except Exception as e:
            self.log.error("错误---,原因{}".format(e))
            # 截图
            self.ckTop.base_get_img()
            # 抛异常
            raise
        finally:
            TestWebYhhd.driver3.quit()

        # test_010,卡片交易(超时自动成功),test_011,卡片交易(打回编辑修改成功),test_012,卡片交易(修改成功后编辑) 超时操作不太好完成

    # 代付交易(成功) 25000奈拉
    def test09_yehd(self):
        try:
            # 获取前余额
            ckye_before = get_db_ye(config.CREDENTIALS["username_ck"])
            dfye_before = get_db_ye_df(config.CREDENTIALS["username_df"])
            TestWebYhhd.driver4 = UtilsDriver4.get_web_driver()
            TestWebYhhd.driver4.get(config.URL_DF)
            TestWebYhhd.dfJd = WebDfJd(TestWebYhhd.driver4)
            login_with_session_df('system4', TestWebYhhd.driver4)
            TestWebYhhd.dfJd.df_yy()
            TestWebYhhd.dfJd.df_jd_click_dfjycl()
            TestWebYhhd.dfJd.df_jd_click_ksjd()
            pyautogui.hotkey('alt', 'tab')
            self.ckDf.ck_df_click_jdf()
            self.ckDf.ck_df("Kuda", "Raheem Afeez Olamilekan", "2050181179", "25000")
            self.ckDf.ck_df_click_tj()
            pyautogui.hotkey('alt', 'tab')
            TestWebYhhd.dfJd.df_jd_click_qrck()
            TestWebYhhd.dfJd.df_jd_qrcg(TestWebYhhd.driver4)
            time.sleep(2)
            # 获取后余额
            ckye_after = get_db_ye(config.CREDENTIALS["username_ck"])
            dfye_after = get_db_ye_df(config.CREDENTIALS["username_df"])
            assert round((float(ckye_before) - float(ckye_after)), 2) == 189.97
            assert round((float(dfye_before) - float(dfye_after)), 2) == -189.97
        except Exception as e:
            self.log.error("错误---,原因{}".format(e))
            # 截图
            self.ckTop.base_get_img()
            # 抛异常
            raise

    # 代付交易(拒绝退款)
    def test10_yehd(self):
        try:
            TestWebYhhd.dfTop = WebDfTop(TestWebYhhd.driver4)
            # 获取前余额
            ckye_before = get_db_ye(config.CREDENTIALS["username_ck"])
            dfye_before = get_db_ye_df(config.CREDENTIALS["username_df"])
            pyautogui.hotkey('alt', 'tab')  # 出卡商
            self.ckDf.ck_df_click_df()
            self.ckDf.ck_df_get_dfgl()
            self.ckDf.ck_tksq()
            pyautogui.hotkey('alt', 'tab')  # 代付商
            time.sleep(2)
            TestWebYhhd.dfTop.ck_df_click_sx()
            TestWebYhhd.dfJd.df_jd_jjtk()
            # 获取后余额
            ckye_after = get_db_ye(config.CREDENTIALS["username_ck"])
            dfye_after = get_db_ye_df(config.CREDENTIALS["username_df"])
            assert round((float(ckye_before) - float(ckye_after)), 2) == 189.97
            assert round((float(dfye_before) - float(dfye_after)), 2) == -189.97
        except Exception as e:
            self.log.error("错误---,原因{}".format(e))
            # 截图
            self.ckTop.base_get_img()
            # 抛异常
            raise

    # 代付交易(同意退款)
    def test11_yehd(self):
        try:
            # 获取前余额
            ckye_before = get_db_ye(config.CREDENTIALS["username_ck"])
            dfye_before = get_db_ye_df(config.CREDENTIALS["username_df"])
            pyautogui.hotkey('alt', 'tab')
            self.ckDf.ck_df_click_df()
            self.ckDf.ck_df_get_dfgl()
            self.ckDf.ck_tksq()
            pyautogui.hotkey('alt', 'tab')  # 代付商
            time.sleep(2)
            TestWebYhhd.dfTop.ck_df_click_sx()
            TestWebYhhd.dfJd.df_jd_tytk()
            # 获取后余额
            ckye_after = get_db_ye(config.CREDENTIALS["username_ck"])
            dfye_after = get_db_ye_df(config.CREDENTIALS["username_df"])
            assert round((float(ckye_before) - float(ckye_after)), 2) == -189.97
            assert round((float(dfye_before) - float(dfye_after)), 2) == 189.97
        except Exception as e:
            self.log.error("错误---,原因{}".format(e))
            # 截图
            self.ckTop.base_get_img()
            # 抛异常
            raise

    # 代付交易(失败)
    def test12_yehd(self):
        try:
            # 获取前余额
            ckye_before = get_db_ye(config.CREDENTIALS["username_ck"])
            dfye_before = get_db_ye_df(config.CREDENTIALS["username_df"])
            pyautogui.hotkey('alt', 'tab')
            self.ckDf.ck_df_click_jdf()
            self.ckDf.ck_df("Kuda", "Raheem Afeez Olamilekan", "2050181179", "25000")
            self.ckDf.ck_df_click_tj()
            pyautogui.hotkey('alt', 'tab')
            TestWebYhhd.dfJd.df_jd_click_qrck()
            TestWebYhhd.dfJd.df_jd_qrsb(TestWebYhhd.driver4)
            time.sleep(2)
            # 获取后余额
            ckye_after = get_db_ye(config.CREDENTIALS["username_ck"])
            dfye_after = get_db_ye_df(config.CREDENTIALS["username_df"])
            assert round((float(ckye_before) - float(ckye_after)), 2) == 0.00
            assert round((float(dfye_before) - float(dfye_after)), 2) == 0.00
        except Exception as e:
            self.log.error("错误---,原因{}".format(e))
            # 截图
            self.ckTop.base_get_img()
            # 抛异常
            raise
